using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace common
{
    public partial class GrigliaGenerica : System.Web.UI.UserControl
    {
        public delegate void SelectEventHandler(string commandName,GenericListDTO row);
        public event SelectEventHandler Select;

        private int numColonne=9;
        public int NumColonne
        {
            get { return numColonne; }
            set { if(value>9)numColonne=9;
                  else if(value<0) numColonne = 0;
                  else numColonne=value;
            for (int i = numColonne; i < 9;i++ )
            {
                DG.Columns[i].Visible = false;
            }
            }
        }

      

        public string H0
        {
            get { return DG.Columns[1].HeaderText; }
            set { DG.Columns[1].HeaderText = value;}
        }
        public string H1
        {
            get { return DG.Columns[2].HeaderText; }
            set { DG.Columns[2].HeaderText = value; }
        }
        public string H2
        {
            get { return DG.Columns[3].HeaderText; }
            set { DG.Columns[3].HeaderText = value; }
        }
        public string H3
        {
            get { return DG.Columns[4].HeaderText; }
            set { DG.Columns[4].HeaderText = value; }
        }
        public string H4
        {
            get { return DG.Columns[5].HeaderText; }
            set { DG.Columns[5].HeaderText = value; }
        }
        public string H5
        {
            get { return DG.Columns[6].HeaderText; }
            set { DG.Columns[6].HeaderText = value; }
        }
        public string H6
        {
            get { return DG.Columns[7].HeaderText; }
            set { DG.Columns[7].HeaderText = value; }
        }
        public string H7
        {
            get { return DG.Columns[8].HeaderText; }
            set { DG.Columns[8].HeaderText = value; }
        }

        private bool viewDelete = false;
        private bool viewSelect = false;
        private bool viewEdit = false;
       
        public bool ViewEdit
        {
            get { return viewEdit; }
            set { viewEdit = value; }
        }
        public bool ViewSelect
        {
            get { return viewSelect; }
            set { viewSelect = value; }
        }
        public bool ViewDelete
        {
            get { return viewDelete; }
            set { viewDelete = value; }
        }
 
        public virtual IList<GenericListDTO> DataSource
        {
            set
            {
                DG.DataSource = value;
                if (Session[this.ClientID] != null) Session[this.ClientID] = DG.DataSource;
                else Session.Add(this.ClientID, DG.DataSource);
                DG.DataBind();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[this.ClientID] != null)
            {
                DG.DataSource = Session[this.ClientID];
                DG.DataBind();
                Panel p = (Panel)this.Parent;
                if (p != null && p.ID == "Container")
                {
                    p.Visible = true;
                }
            }
            else
            {
                Panel p = (Panel)this.Parent;
                if (p != null && p.ID == "Container")
                {
                    p.Visible = false;
                }
            }
        }

      
        protected void DG_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("SELECT"))
            {
                Select("SELECT", new GenericListDTO(e.CommandArgument.ToString(),null,null,null,null,null,null,null,null));
                Clear();
            }
            else if (e.CommandName == "EDIT")
            {
                Select("EDIT", new GenericListDTO(e.CommandArgument.ToString(), null, null, null, null, null, null, null, null));
                Clear();
            }
            else if (e.CommandName == "DELETE")
            {
                Select("DELETE", new GenericListDTO(e.CommandArgument.ToString(), null, null, null, null, null, null, null, null));
                Clear();
            }
        }

        private string ConvertSortDirection(SortDirection sortDirection, string SortColumn)
        {
            string sortExpression = String.Empty;
            if (SortColumn == (string)HttpContext.Current.Session["SortColumn"])
            {     
                // We are resorting the same column, so flip the sort direction   
                sortDirection = ((SortDirection)HttpContext.Current.Session["SortColumnDirection"] == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;
            } 
            // Apply the sort    
            
            sortExpression =  SortColumn + (string)((sortDirection == SortDirection.Ascending) ? " ASC" : " DESC");
            HttpContext.Current.Session["SortColumn"] = SortColumn;
            HttpContext.Current.Session["SortColumnDirection"] = sortDirection;
            return sortExpression;
        }

        protected void DG_Sorting(object sender, GridViewSortEventArgs e)
        {
            //TODO:cambiare IList<AsmGasContatoreEntity> l = (IList<AsmGasContatoreEntity>)DG.DataSource;
            DG.DataSource = null;
            DG.DataBind();
        }
    
        protected void DG_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DG.PageIndex = e.NewPageIndex;
            DG.DataBind();
        }

        public bool SessionFull()
        {
            if (Session[this.ClientID] == null) return false;
            else return true;
        }

        public void Clear()
        {
            if (Session[this.ClientID] != null) Session.Remove(this.ClientID);
        }
     
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (DG.Rows.Count == 0)
            {

                lbl_vuoto.Visible = true; DG.Visible = false;
            }
            else { 
                lbl_vuoto.Visible = false; 
                DG.Visible = true; 
                 }
        }
    
    }
}